using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Globalization;

namespace Sixeyed.CacheAdapter.Management.Converters
{
    /// <summary>
    /// Convertor for cache provider configuration settings
    /// </summary>
    public class CacheProviderConfiguration : StringConverter
    {
        /// <summary>
        /// Converts the value by loading as XML and returning the root element name
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (typeof(string) == destinationType && value is string)
            {
                XmlDocument configDocument = new XmlDocument();
                configDocument.LoadXml((string)value);
                return configDocument.DocumentElement.Name;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        /// <summary>
        /// Returns whether the convertor can convert from given type
        /// </summary>
        /// <remarks>
        /// Logic not implemented
        /// </remarks>
        /// <param name="context"></param>
        /// <param name="sourceType"></param>
        /// <returns>False</returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return false;
        }

        /// <summary>
        /// Returns whether values are exclusive
        /// </summary>
        /// <remarks>
        /// Logic not implemented
        /// </remarks>
        /// <param name="context"></param>
        /// <returns>True</returns>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
   }
}
